package com.dynamixsoftware.printershare;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.net.Uri.Builder;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import com.dynamixsoftware.printershare.ActivityPrintMessages;
import com.dynamixsoftware.printershare.ActivityRoot;
import com.dynamixsoftware.printershare.App;
import com.dynamixsoftware.printershare.ContactInfoCache;
import com.google.android.mms.pdu.CharacterSets;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityMessages extends ActivityRoot {

	static final StyleSpan STYLE_BOLD = new StyleSpan(1);
	private static final Map sRecipientAddress = new ConcurrentHashMap(20);
	private ContactInfoCache cache;
	private ListView mList;

	private static String extractEncStrFromCursor(String var0, int var1) {
		if (TextUtils.isEmpty(var0)) {
			var0 = "";
		} else if (var1 != 0) {
			try {
				String var2 = new String(var0.getBytes("iso-8859-1"), CharacterSets.getMimeName(var1));
				return var2;
			} catch (UnsupportedEncodingException var4) {
				return var0;
			}
		}

		return var0;
	}

	private static StringBuilder extractIdsToAddresses(Context var0, String var1, boolean var2) {
		StringBuilder var3 = new StringBuilder();
		String[] var4 = var1.split(" ");
		boolean var5 = true;
		int var6 = var4.length;
		int var7 = 0;

		while (true) {
			if (var7 >= var6) {
				if (var3.length() == 0) {
					return null;
				}
				break;
			}

			String var8 = var4[var7];
			String var9 = (String) sRecipientAddress.get(var8);
			if (var9 == null) {
				if (!var2) {
					var3 = null;
					break;
				}

				Uri var12 = Uri.parse("content://mms-sms/canonical-address/" + var8);

				Cursor var14;
				label96: {
					Cursor var17;
					try {
						var17 = var0.getContentResolver().query(var12, (String[]) null, (String) null, (String[]) null,
								(String) null);
					} catch (Exception var21) {
						var21.printStackTrace();
						App.reportThrowable(var21);
						var14 = null;
						break label96;
					}

					var14 = var17;
				}

				if (var14 != null) {
					try {
						if (var14.moveToFirst()) {
							var9 = var14.getString(0);
							sRecipientAddress.put(var8, var9);
						}
					} finally {
						var14.close();
					}
				}
			}

			if (var9 != null) {
				if (var5) {
					var5 = false;
				} else {
					var3.append(";");
				}

				var3.append(var9);
			}

			++var7;
		}

		return var3;
	}

	private static String getRecipientsByIds(Context var0, String var1, boolean var2) {
		String var3 = (String) sRecipientAddress.get(var1);
		if (var3 != null) {
			return var3;
		} else {
			String var4;
			if (!TextUtils.isEmpty(var1)) {
				StringBuilder var6 = extractIdsToAddresses(var0, var1, var2);
				if (var6 == null) {
					return "";
				}

				var4 = var6.toString();
			} else {
				var4 = "";
			}

			sRecipientAddress.put(var1, var4);
			return var4;
		}
	}

	public void onCreate(Bundle var1) {
		super.onCreate(var1);
		this.setContentView(2130903060);
		this.setTitle(2131099679);
		this.mList = (ListView) this.findViewById(2131230725);
		this.mList.setChoiceMode(2);
		this.mList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView var1, View var2, int var3, long var4) {
				View var6;
				if (var2 != null) {
					var6 = var2.findViewById(2131230734);
				} else {
					var6 = null;
				}

				if (var6 != null) {
					byte var7;
					if (ActivityMessages.this.mList.isItemChecked(var3)) {
						var7 = 0;
					} else {
						var7 = 8;
					}

					var6.setVisibility(var7);
				}

			}
		});
		((Button) this.findViewById(2131230726)).setOnClickListener(new OnClickListener() {
			public void onClick(View var1) {
				SparseBooleanArray var2 = ActivityMessages.this.mList.getCheckedItemPositions();
				String var3 = "";

				for (int var4 = 0; var4 < var2.size(); ++var4) {
					if (var2.valueAt(var4)) {
						ActivityMessages.MessageHeader var8 = (ActivityMessages.MessageHeader) ((ActivityMessages.MessageHeader) ActivityMessages.this.mList
								.getAdapter().getItem(var2.keyAt(var4)));
						StringBuilder var9 = (new StringBuilder()).append(var3);
						String var10;
						if (var3.length() > 0) {
							var10 = "\n";
						} else {
							var10 = "";
						}

						var3 = var9.append(var10).append(var8.thread_id).append(" ").append(var8.from).toString();
					}
				}

				if (var3.length() > 0) {
					Intent var5 = new Intent();
					var5.setClass(ActivityMessages.this, ActivityPrintMessages.class);
					var5.putExtra("data", var3);
					ActivityMessages.this.startActivity(var5);
				} else {
					Toast.makeText(ActivityMessages.this, 2131099683, 1).show();
				}
			}
		});
		this.cache = new ContactInfoCache(this);
		Builder var2 = Uri.parse("content://mms-sms/conversations").buildUpon();
		var2.appendQueryParameter("simple", "true");
		Uri var4 = var2.build();
		Cursor var5 = this.getContentResolver().query(
				var4,
				new String[] { "_id", "message_count", "recipient_ids", "date", "read", "snippet", "snippet_cs",
						"error" }, (String) null, (String[]) null, "date DESC");
		Vector var6 = new Vector();
		ConcurrentHashMap var7 = new ConcurrentHashMap();
		ActivityMessages.CachingNameStore var8 = new ActivityMessages.CachingNameStore(this);
		if (var5 != null) {
			while (var5.moveToNext()) {
				ActivityMessages.MessageHeader var9 = new ActivityMessages.MessageHeader();
				var9.thread_id = var5.getLong(0);
				String var10 = var5.getString(2);
				String var11 = getRecipientsByIds(this, var10, true);
				String var12 = (String) var7.get(var10);
				if (var12 == null) {
					var12 = var8.getContactNames(var11);
					if (var12 == null) {
						var12 = "";
					}

					var7.put(var10, var12);
				}

				var9.from = var12;
				var9.subject = extractEncStrFromCursor(var5.getString(5), var5.getInt(6));
				var9.date = App.formatTimeStampString(this, var5.getLong(3), false);
				boolean var13;
				if (var5.getInt(4) != 0) {
					var13 = true;
				} else {
					var13 = false;
				}

				var9.read = var13;
				boolean var14;
				if (var5.getInt(7) != 0) {
					var14 = true;
				} else {
					var14 = false;
				}

				var9.error = var14;
				var9.message_count = var5.getInt(1);
				var6.add(var9);
			}

			var5.close();
		}

		if (var6.size() > 0) {
			this.mList.setAdapter(new ActivityMessages.MessageAdapter(this, var6));
			this.mList.setVisibility(0);
		} else {
			this.mList.setVisibility(8);
		}
	}

	public boolean onCreateOptionsMenu(Menu var1) {
		var1.add(0, 1, 0, 2131099661);
		var1.add(0, 2, 0, 2131099662);
		return true;
	}

	public boolean onMenuItemSelected(int var1, MenuItem var2) {
		boolean var3 = true;
		ListAdapter var4 = this.mList.getAdapter();
		if (var4 != null) {
			switch (var2.getItemId()) {
			case 1:
				for (int var6 = 0; var6 < var4.getCount(); ++var6) {
					this.mList.setItemChecked(var6, var3);
				}

				return var3;
			case 2:
				for (int var5 = 0; var5 < var4.getCount(); ++var5) {
					this.mList.setItemChecked(var5, false);
				}

				return var3;
			}
		}

		var3 = false;
		return var3;
	}

	private final class CachingNameStore {

		private final ConcurrentHashMap mCachedNames = new ConcurrentHashMap();

		public CachingNameStore(Context var2) {
		}

		public String getContactNames(String var1) {
			String var2 = (String) this.mCachedNames.get(var1);
			if (var2 != null) {
				return var2;
			} else {
				String[] var3 = var1.split(";");
				String var9;
				if (var3.length < 2) {
					var9 = ActivityMessages.this.cache.getContactName(var1).replace(';', ',');
				} else {
					int var4 = 0;

					for (int var5 = 0; var5 < var3.length; ++var5) {
						var3[var5] = this.getContactNames(var3[var5]);
						var4 += 2 + var3[var5].length();
					}

					StringBuilder var6 = new StringBuilder(var4);
					var6.append(var3[0]);

					for (int var8 = 1; var8 < var3.length; ++var8) {
						var6.append(", ");
						var6.append(var3[var8]);
					}

					var9 = var6.toString();
				}

				this.mCachedNames.put(var1, var9);
				return var9;
			}
		}
	}

	class MessageAdapter extends BaseAdapter {

		private Vector mDat;

		public MessageAdapter(Context var2, Vector var3) {
			this.mDat = var3;
		}

		private CharSequence formatMessage(ActivityMessages.MessageHeader var1) {
			SpannableStringBuilder var2 = new SpannableStringBuilder(var1.from);
			if (var1.message_count > 1) {
				var2.append(" (" + var1.message_count + ") ");
			}

			if (!var1.read) {
				var2.setSpan(ActivityMessages.STYLE_BOLD, 0, var2.length(), 17);
			}

			return var2;
		}

		public int getCount() {
			return this.mDat.size();
		}

		public Object getItem(int var1) {
			return this.mDat.get(var1);
		}

		public long getItemId(int var1) {
			return (long) var1;
		}

		public View getView(int var1, View var2, ViewGroup var3) {
			if (var2 == null) {
				var2 = ActivityMessages.this.getLayoutInflater().inflate(2130903061, var3, false);
			}

			TextView var4 = (TextView) var2.findViewById(2131230727);
			TextView var5 = (TextView) var2.findViewById(2131230728);
			TextView var6 = (TextView) var2.findViewById(2131230774);
			View var7 = var2.findViewById(2131230788);
			ActivityMessages.MessageHeader var8 = (ActivityMessages.MessageHeader) this.getItem(var1);
			var4.setText(this.formatMessage(var8));
			byte var9;
			if (var8.read) {
				var9 = 4;
			} else {
				var9 = 0;
			}

			var7.setVisibility(var9);
			var5.setText(var8.subject);
			var6.setText(var8.date);
			View var10 = var2.findViewById(2131230734);
			boolean var11 = ActivityMessages.this.mList.isItemChecked(var1);
			byte var12 = 0;
			if (!var11) {
				var12 = 8;
			}

			var10.setVisibility(var12);
			return var2;
		}
	}

	class MessageHeader {

		public String date;
		public boolean error;
		public String from;
		public int message_count;
		public boolean read;
		public String subject;
		public long thread_id;

	}
}
